<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<%@ taglib prefix="t" uri="http://myfaces.apache.org/tomahawk" %>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Остатки на складе</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <link rel="stylesheet" type="text/css" href="resources/css/jquery.countdown.css">
    <script type="text/javascript">
        function reloadPage() {
            window.location.reload();
        }
    </script>
    <style type="text/css">
        .field
        {
            clear:both;
            text-align:right;
            margin-top: 5px;
        }

        label {float: left}

        .main
        {
            float: left;
            clear:both;
            width: 100%;
        }

        .rich-table-cell{
            font-size: 1.1em;
            text-align: center;
        }

    </style>
</head>
<body>

<f:view>
    <jsp:include page="StorageMenu.jsp"/>
    <jsp:include page="status.jsp"/>
    <a4j:form id="jsForm" prependId="false">
        <a4j:jsFunction name="refresh" action="#{StorageEnteranceBean.reloadData}" limitToList="true"
                        reRender="goodsTable, form"/>
    </a4j:form>
    <h:form>
        <!-- Фильтр -->
        <h:outputText value="Фильтр: " styleClass="b"/>
        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Код"/>
            <h:inputText value="#{StorageEnteranceBean.filter.code}" styleClass="f_tb_s"/>
        </h:panelGroup>
        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Наименование"/>
            <h:inputText value="#{StorageEnteranceBean.filter.name}" styleClass="f_tb_l"/>
        </h:panelGroup>
        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Группа товара"/>
            <h:selectOneMenu value="#{StorageEnteranceBean.filter.goodTypeId}" styleClass="f_s">
                <f:selectItems value="#{StorageEnteranceBean.goodTypesAsSelectItems}"/>
            </h:selectOneMenu>
        </h:panelGroup>
        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Только различия"/>
            <h:selectBooleanCheckbox value="#{StorageEnteranceBean.filter.onlyDifferense}"/>
        </h:panelGroup>
        <a4j:commandButton reRender="goodsTable" value="Применить" action="#{StorageEnteranceBean.applyFilter}"/>
        <br />
    </h:form>

    <h:form>
        <h:commandLink value="Выгрузить в Excel" action="#{StorageEnteranceBean.exportToExcel}" style="margin-top: 10px; font-size: 1.1em;"/>
    </h:form>

    <h:form>
        <rich:dataTable id="goodsTable" value="#{StorageEnteranceBean.goods}" var="good" rowKeyVar="row" rows="30" style="margin-top: 20px; width: 60%; text-align: center;">
            <f:facet name="header">
                <rich:datascroller id="datascroller"/>
            </f:facet>

            <rich:column styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="№"/>
                </f:facet>
                <h:outputText value="#{row + 1}"/>
            </rich:column>

            <rich:column styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Действия"/>
                </f:facet>
                <a4j:commandLink ajaxSingle="true" oncomplete="Richfaces.showModalPanel('addGood')" title="Поставить на приход">
                    <h:graphicImage value="resources/img/add_16.png"/>
                    <f:setPropertyActionListener value="0" target="#{StorageEnteranceBean.operation}"/>
                    <f:setPropertyActionListener value="#{good.code}" target="#{StorageEnteranceBean.goodCode}"/>
                </a4j:commandLink>
                <a4j:commandLink ajaxSingle="true" oncomplete="Richfaces.showModalPanel('addGood')" style="margin-left: 20px;" title="Списать">
                    <h:graphicImage value="resources/img/remove_16x16.png"/>
                    <f:setPropertyActionListener value="1" target="#{StorageEnteranceBean.operation}"/>
                    <f:setPropertyActionListener value="#{good.code}" target="#{StorageEnteranceBean.goodCode}"/>
                </a4j:commandLink>
            </rich:column>ss

            <rich:column styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Код товара"/>
                </f:facet>
                <h:outputText value="#{good.code}"/>
            </rich:column>

            <rich:column styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Остатки на складе"/>
                </f:facet>
                <h:outputText value="#{good.storageCount}"/>
            </rich:column>

            <rich:column styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Остатки в 1С"/>
                </f:facet>
                <h:outputText value="#{good.count}"/>
            </rich:column>

            <rich:column sortable="true" sortBy="#{good.goodCountDifference}" styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Излишки"/>
                </f:facet>
                <h:outputText value="#{good.storageCount > good.count ? good.goodCountDifference : 0}"/>
            </rich:column>

            <rich:column sortable="true" sortBy="#{good.goodCountDifference}" styleClass="#{good.count != good.storageCount ? 'little':''}">
                <f:facet name="header">
                    <h:outputText value="Недостатки"/>
                </f:facet>
                <h:outputText value="#{good.storageCount < good.count ? good.goodCountDifference : 0}"/>
            </rich:column>

            <f:facet name="footer">
                    <rich:columnGroup>
                        <rich:column/>
                        <rich:column/>
                        <rich:column/>
                        <rich:column/>
                        <rich:column/>
                        <rich:column>
                            <p>
                                <h:outputText value="Штук: #{StorageEnteranceBean.overflowCount}"/><br/>
                                <h:outputText value="Сумма: #{StorageEnteranceBean.overflowSum}"/>
                            </p>
                        </rich:column>
                        <rich:column>
                            <p>
                                <h:outputText value="Штук: #{StorageEnteranceBean.shortageCount}"/><br/>
                                <h:outputText value="Сумма: #{StorageEnteranceBean.shortageSum}"/>
                            </p>
                        </rich:column>
                    </rich:columnGroup>
            </f:facet>
        </rich:dataTable>
    </h:form>

    <rich:modalPanel id="addGood" width="400" height="270">
        <f:facet name="header">
            <a4j:outputPanel ajaxRendered="true" style="text-align: center;">
                <h:outputText value="#{StorageEnteranceBean.operationAsString}"/>
            </a4j:outputPanel>
        </f:facet>
        <h:form id="form">
            <jsp:include page="messages.jsp"/>
            <h:panelGrid columns="2" styleClass="full" columnClasses="dleft,dright" cellspacing="0" cellpadding="0">
                    <h:outputText value="Код товара"/>
                    <a4j:outputPanel ajaxRendered="true">
                        <h:outputText value="#{StorageEnteranceBean.goodCode}"/>
                    </a4j:outputPanel>
                    <h:outputText value="Количество"/>
                    <h:inputText value="#{StorageEnteranceBean.goodCount}"
                                 required="true"
                                 requiredMessage="Поле \"Количество\" обязательно для заполнения"
                                 converterMessage="Поле \"Количество\" должно быть числом"
                                 validatorMessage="Количество должно быть больше 0"
                                 >
                        <f:convertNumber integerOnly="true" type="number"/>
                        <f:validateLongRange minimum="1"/>
                    </h:inputText>
                    <h:outputText value="Комментарий"/>
                    <h:inputTextarea value="#{StorageEnteranceBean.comment}"
                                     required="true"
                                     requiredMessage="Поле \"Комментарий\" обязательно для заполнения"
                                     style="height: 120px;">
                    </h:inputTextarea>
                </h:panelGrid>
                <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0" style="text-align: end;">
                    <a4j:commandButton value="Сохранить" action="#{StorageEnteranceBean.saveStorageEnterance}"
                                       oncomplete="if(#{!StorageEnteranceBean.hasFacesErrors}) {Richfaces.hideModalPanel('addGood');refresh();}" styleClass="btnsz" reRender="goodsTable, form"/>
                    <a4j:commandButton value="Отмена" onclick="Richfaces.hideModalPanel('addGood');refresh();" styleClass="btnsz"/>
                </h:panelGrid>
        </h:form>
    </rich:modalPanel>
</f:view>

</body>
</html>
